#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#define N 10010
#define ri register int
using namespace std;

int n, m, num, ans, cnt;
int out[N];
int same[N], tot[N];
int dfn[N], low[N], ins[N];
stack<int> stk;

int e_cnt, heads[N];
struct Edge {
	int v, nxt;
} e[50000+5];

inline void add(int u, int v) {
	e[++e_cnt].v = v;
	e[e_cnt].nxt = heads[u];
	heads[u] = e_cnt;
}

void tarjan(int x) {
	dfn[x] = low[x] = ++num;
	stk.push(x);
	ins[x] = true;

	for (ri i = heads[x]; i; i = e[i].nxt) {
		if (!dfn[e[i].v]) {
			tarjan(e[i].v);
			low[x] = min(low[x], low[e[i].v]);
		} else if (ins[e[i].v]) {
			low[x] = min(low[x], dfn[e[i].v]);
		}
	}
	if (dfn[x] == low[x]) {
		cnt++;
		int y;
		do {
			y = stk.top();
			ins[y] = false;
			same[y] = cnt;
			tot[cnt]++;
			stk.pop();
		} while (y != x);
	}
}

int main() {
	// freopen("testdata.in", "r", stdin);
	// freopen("testdata.out", "w", stdout);

	scanf("%d%d", &n, &m);
	int tmp, tmp2;
	for (ri i = 1; i <= m; ++i) {
		scanf("%d%d", &tmp, &tmp2);
		add(tmp, tmp2);
	}
	for (ri i = 1; i <= n; ++i) {
		if (!dfn[i]) {
			tarjan(i);
		}
	}
	for (ri i = 1; i <= n; ++i) {
		for (int j = heads[i]; j; j = e[j].nxt) {
			if (same[i] != same[e[j].v]) {
				out[same[i]]++;
			}
		}
	}
	for (ri i = 1; i <= cnt; ++i) {
		if (!out[i]) {
			if (ans) {
				printf("0");
				return 0;
			}
			ans = i;
		}
	}
	printf("%d\n", tot[ans]);
	// fclose(stdin);
	// fclose(stdout);
	return 0;
}